<?php
namespace Common\Common;
/**
 * 通过主键获取对象
 * @return Model
 */
abstract class Model extends \Think\Model{
    protected $odjectClass;
    /**
     *通过逐渐获取对象
     *@param int|string $key
     *@return static
     */
//    public function fetch($key){
//        $data=$this->where([$this->tablePrimary=>$key])->find();
//        $className=$this->odjectClass;
//        return new  $className($data);
//    }
    public function fetch(){
        $args = func_get_args();
        if(is_string($this->tablePrimary))
            $this->tablePrimary = [$this->tablePrimary];
        $condition = [];
        foreach ($this->tablePrimary as $key => $value)
            $condition[$value] = $args[$key];
        $data = $this->where($condition)->find();
        $className = substr(static::class, 0, -5);
        $className = $className.'Object';
        return new $className($data, $this);
    }

    public function fetch1(){
        $args = func_get_args();
        if(is_string($this->tablePrimary1))
            $this->tablePrimary1 = [$this->tablePrimary1];
        $condition = [];
        foreach ($this->tablePrimary1 as $key => $value)
            $condition[$value] = $args[$key];
        $data = $this->where($condition)->find();
        $className = substr(static::class, 0, -5);
        $className = $className.'Object';
        return new $className($data, $this);
    }

    /**
     * 通过主键获取对象
     * @param $condition
     * @return array
     */
    public function fetchList($condition){
        $dataList = $this->where($condition)->select();
        $result = [];
        $className = substr(static::class, 0, -5);
        $className = $className.'Object';
        foreach ($dataList as $value){
            $result[] = new $className($value, $this);
        }
        return $result;
    }

    /**

     * 通过主键获取对象

     * @param $modelName

     * @param $data

     * @return $res

     */
    public function add($modelName,$data){
        $Model = M($modelName); // 实例化对象
        $res = $Model->add($data);
        return $res;
    }

    public function del($modelName,$id){
        $Model = M($modelName); // 实例化对象
        return $Model->delete($id);
    }

    public function make(array $data=[]){
        $className = substr(static::class, 0, -5);
        $className = $className.'Object';
        return new $className($data, $this);
    }

}